import pygtk
pygtk.require('2.0')
import gtk
import os


dirapps = '/usr/share/applications/'
dirpixmaps = '/usr/share/pixmaps/'

class ComboBoxApps:
    def __init__(self):
        global textbuffer, iconfile, image, label
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect('destroy', lambda w: gtk.main_quit())
        window.set_title('Add Desktop Apps')
        
        #box1 = gtk.VBox(False, 0)
        box1 = gtk.VBox(False, 0)
        window.add(box1)
        box1.show()

        box1b = gtk.HBox()        
        box1.add(box1b)
        box1b.show()

        image = gtk.Image()
        image.show()
        box1b.add(image)

        combobox = gtk.combo_box_new_text()
        box1b.add(combobox)
        combobox.set_wrap_width(3)
        combobox.append_text('Select file...')
        for f in sorted(os.listdir(dirapps)):
            if os.path.isfile(os.path.join(dirapps, f)):
                combobox.append_text(f)
        combobox.connect('changed', self.changed_cb)
        combobox.set_active(0)
        
        label = gtk.Label('Icon')
        label.set_justify(gtk.JUSTIFY_LEFT)
        box1.add(label)
        
        box2 = gtk.VBox(False, 10)
        box2.set_border_width(10)
        box1.pack_start(box2, True, True, 0)
        box2.show()
        
        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        sw.set_size_request(700, 400)
        textview = gtk.TextView()
        textbuffer = textview.get_buffer()
        sw.add(textview)
        sw.show()
        textview.show()
        
        box2.pack_start(sw)
        
        window.show_all()
        return

    def changed_cb(self, combobox):
        global textbuffer, iconfile, image
        model = combobox.get_model()
        index = combobox.get_active()
        if index:
            image.clear()
            print 'File', model[index][0], 'selected'
            # Load the file into the text window
            infile = open(os.path.join(dirapps,model[index][0]), "r")
            if infile:
                string = infile.read()
                infile.seek(0)
                lines = infile.readlines()
                infile.close()
                textbuffer.set_text(string)
                for line in lines:
                    #print 'line: ', line,
                    if line[0:5].lower() == 'icon=':
                        iconfile = line[5:].strip()
                        print 'iconfile:', iconfile,
                        if os.path.exists(os.path.join('/usr/share/icons/gnome/scalable/apps/',iconfile + '.svg')):
                            iconfile = os.path.join('/usr/share/icons/gnome/scalable/apps/',iconfile + '.svg')
                        if os.path.exists(os.path.join(dirpixmaps,iconfile + '.xpm')):
                            iconfile = os.path.join(dirpixmaps,iconfile + '.xpm')
                        if os.path.exists(os.path.join(dirpixmaps,iconfile + '.png')):
                            iconfile = os.path.join(dirpixmaps,iconfile + '.png')
                        if os.path.exists(os.path.join(dirpixmaps,iconfile + '.svg')):
                            iconfile = os.path.join(dirpixmaps,iconfile + '.svg')
                        if os.path.exists(os.path.join(dirpixmaps,iconfile)):
                            iconfile = os.path.join(dirpixmaps,iconfile)
                        image.set_from_file(iconfile)
                        label.set_text(iconfile)
        return

def main():
    gtk.main()
    return

if __name__ == "__main__":
    bcb = ComboBoxApps()
    main()

